package com.livrexpress.livrexpressandroid.localdomain;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class Tour implements Entity{
	
	private static String TABLE = "Tour";
	private static String COL_ID_TOUR = "idTour";
	private static int COL_ID_TOUR_NUM = 0;
    private static String COL_ID_LIVREUR = "idLivreur";
    private static int COL_ID_LIVREUR_NUM = 1;
    private static String COL_DATETOUR = "dateTour";
    private static int COL_DATETOUR_NUM = 2;
    private static String COL_NAME_LIVREUR = "nomLivreur";
    private static int COL_NAME_LIVREUR_NUM = 3;
    
    private int idTour;
    private String idLivreur;
    private String dateTour;
    private String nomLivreur; 
    
	public Tour() {
		super();
		this.idTour = 0;
		this.idLivreur = " ";
		this.dateTour = " ";
		this.nomLivreur =" ";
	}

	public int getIdTour() {
		return idTour;
	}

	private void setIdTour(int idTour) {
		this.idTour = idTour;
	}

	public String getIdLivreur() {
		return idLivreur;
	}

	public void setIdLivreur(String idLivreur) {
		this.idLivreur = idLivreur;
	}

	public String getDateTour() {
		return dateTour;
	}

	public void setDateTour(String dateTour) {
		this.dateTour = dateTour;
	}

	public String getNomLivreur() {
		return nomLivreur;
	}

	public void setNomLivreur(String nomLivreur) {
		this.nomLivreur = nomLivreur;
	}

	@Override
	public void createTable(SQLiteDatabase db) {
		String CREATE_TABLE = "CREATE TABLE " + TABLE + " ("
				+ COL_ID_TOUR + " INTEGER PRIMARY KEY AUTOINCREMENT, "
			        + COL_ID_LIVREUR + " TEXT, "
			        + COL_DATETOUR + " TEXT, "
			        + COL_NAME_LIVREUR + " TEXT);";
			        db.execSQL(CREATE_TABLE);
	}

	@Override
	public void removeTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE);
	}

	@Override
	public void insert() {
		ContentValues val = new ContentValues();
		val.put(COL_ID_LIVREUR, idLivreur);
        val.put(COL_DATETOUR, dateTour);
        val.put(COL_NAME_LIVREUR, nomLivreur);
        LivreApplication.getSqliteManager().insert(TABLE, val);
        getLastTourInsert();
		
	}

	@Override
	public void update() {
		ContentValues val = new ContentValues();
		val.put(COL_ID_LIVREUR, idLivreur);
        val.put(COL_DATETOUR, dateTour);
        val.put(COL_NAME_LIVREUR, nomLivreur);
        LivreApplication.getSqliteManager().update(TABLE, val, COL_ID_TOUR+" = '"+this.idTour+"'");
		
	}

	@Override
	public void delete() {
		LivreApplication.getSqliteManager().delete(TABLE, COL_ID_TOUR + " = " +this.idTour);
		
	}
	public void getLastTourInsert(){
        Cursor result = null;
        try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM "+TABLE+" WHERE "+COL_DATETOUR+" = '"+this.dateTour+"' AND "+COL_ID_LIVREUR+" = '"+this.idLivreur+"'" , null);
        }
        catch(Exception e)
        {
            
        }
        if(!result.moveToNext())
        {
            result.close();
           
        }
        
        this.idTour = result.getInt(COL_ID_TOUR_NUM);
        
        result.close();
		
	}
	
	public static Tour getTourById(int tourId){
		Cursor result = null;
		try
        {
            result = LivreApplication.getSqliteManager().rawQuery("SELECT * FROM "+TABLE+" WHERE "+COL_ID_TOUR+" = '"+tourId+"'" , null);
        }
        catch(Exception e)
        {
            
        }
		if(!result.moveToNext())
        {
            result.close();
            return null;
        }
		Tour t = new Tour();
		t.setDateTour(result.getString(COL_DATETOUR_NUM));
		t.setIdLivreur(result.getString(COL_ID_LIVREUR_NUM));
		t.setNomLivreur(result.getString(COL_NAME_LIVREUR_NUM));
		t.setIdTour(tourId);
		
		return t;
		
	}

}
